﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
namespace DAO
{
    public class DAO_DoanhNghiep
    {
        public string GetDiaChi(int MaDN)
        {
            string queryGetDetails = "SELECT DiaChi FROM DOANHNGHIEP WHERE MaDN = " + MaDN.ToString();
            DataTable dt = DataProvider.ExecuteQuery(queryGetDetails);
            DataRow dr = dt.Rows[0];
            return dr[0].ToString();
        }
        public List<DTO_DoanhNghiep> LayDSDN()
        {
            List<DTO_DoanhNghiep> dto_companies = new List<DTO_DoanhNghiep>();
            string queryGetDetails = "SELECT MaDN, TenDoanhNghiep FROM [DOANHNGHIEP]";
            DataTable dt = DataProvider.ExecuteQuery(queryGetDetails);
            foreach (DataRow dr in dt.Rows)
            {
                DTO_DoanhNghiep company = new DTO_DoanhNghiep();
                company.MaDN = int.Parse(dr[0].ToString());   //Ma DN
                company.TenDoanhNghiep = dr[1].ToString();   //Tên DN 
                dto_companies.Add(company);
            }
            return dto_companies;
        }
        public List<DTO_DoanhNghiep> LayThongTinDoanhNghiep(int matk)
        {
            List<DTO_DoanhNghiep> dto_companies = new List<DTO_DoanhNghiep>();
            string queryGetDetails = "SELECT MaDN, TenDoanhNghiep,ChucVu,Website,DiaChi,LoaiHinhKinhDoanh,NoiMuonQuangCao,ThongTinHopTac FROM [DOANHNGHIEP] WHERE ([TaiKhoan] = " + matk + ")";
            DataTable dt = DataProvider.ExecuteQuery(queryGetDetails);
            foreach (DataRow dr in dt.Rows)
            {
                DTO_DoanhNghiep company = new DTO_DoanhNghiep();
                company.MaDN = int.Parse(dr[0].ToString());   //Ma DN
                company.TenDoanhNghiep = dr[1].ToString();   //Tên DN 
                company.ChucVu =dr[2].ToString();
                company.Website = dr[3].ToString();
                company.DiaChi= dr[4].ToString();
                company.LoaiHinhKinhDoanh = int.Parse(dr[5].ToString());
                company.NoiMuonQuangCao = int.Parse(dr[6].ToString());
                company.ThongTinHopTac = dr[7].ToString();
                dto_companies.Add(company);
            }
            return dto_companies;
        }

        public List<DTO_ChiTietDoanhNghiep> LayThongTinChiTietDoanhNghiep(int matk)
        {
            List<DTO_ChiTietDoanhNghiep> dto_companies = new List<DTO_ChiTietDoanhNghiep>();
            string queryGetDetails = "SELECT tk.HoTen, tk.Email, tk.Password,tk.DienThoai, dn.TenDoanhNghiep, dn.ChucVu, dn.Website, dn.DiaChi, dn.LoaiHinhKinhDoanh, kv.TenKhuVuc, dn.ThongTinHopTac From TAIKHOAN tk, DOANHNGHIEP dn, KHUVUC kv Where tk.MaTK = dn.TaiKhoan and tk.MaTK=" + matk.ToString() + " and kv.MaKV = dn.NoiMuonQuangCao";
            DataTable dt = DataProvider.ExecuteQuery(queryGetDetails);
            foreach (DataRow dr in dt.Rows)
            {
                DTO_ChiTietDoanhNghiep company = new DTO_ChiTietDoanhNghiep();
                company.HoTen = dr[0].ToString();
                company.Email = dr[1].ToString();
                company.Password = dr[2].ToString();
                company.DienThoai = dr[3].ToString();
                company.TenDoanhNghiep = dr[4].ToString();
                company.ChucVu = dr[5].ToString();
                company.Website = dr[6].ToString();
                company.DiaChi = dr[7].ToString();
                company.LoaiHinhKinhDoanh = dr[8].ToString();
                company.NoiMuonQuangCao = dr[9].ToString();
                company.ThongTinHopTac = dr[10].ToString();
                dto_companies.Add(company);
            }
            return dto_companies;
        }
        public void Update(DTO_DoanhNghiep dn)
        {
            string query = "UPDATE DOANHNGHIEP SET ChucVu=N'" + dn.ChucVu + "', TenDoanhNghiep=N'" + dn.TenDoanhNghiep + "', Website='" + dn.Website + "',DiaChi=N'" + dn.DiaChi + "',LoaiHinhKinhDoanh=" + dn.LoaiHinhKinhDoanh + ",NoiMuonQuangCao=" + dn.NoiMuonQuangCao + ",ThongTinHopTac=N'" + dn.ThongTinHopTac + "' WHERE ([MaDN] = '" + dn.MaDN + "')";
            DataProvider.ExecuteQuery(query);
        }
        public bool ThemDoanhNghiep(string dnTaikhoan, string chucvu, string name, string website,
                                    string diachi, string loaihinh, string noimuonquangcao, string contactinfo,
                                    string tinhtrang)
        {
            try
            {
                string queryInsert = "INSERT INTO DOANHNGHIEP VALUES ('" +
                                dnTaikhoan + "','" +
                                chucvu + "', N'" +
                                name + "','" +
                                website + "', N'" +
                                diachi + "', N'" +
                                loaihinh + "', '" +
                                noimuonquangcao + "', N'" +
                                contactinfo + "', '" +
                                tinhtrang + "');";
                DataProvider.ExecuteQuery(queryInsert);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool KTDoanhNghiep(string tenDoanhNghiep)
        {
            string query = "Select TenDoanhNghiep from DOANHNGHIEP where TenDoanhNghiep='" + tenDoanhNghiep + "'";
            List<DTO_DoanhNghiep> list = new List<DTO_DoanhNghiep>();
            DataTable TB = DataProvider.ExecuteQuery(query);
            foreach (DataRow dr in TB.Rows)
            {
                DTO_DoanhNghiep dto = new DTO_DoanhNghiep();
                dto.TenDoanhNghiep = dr["TenDoanhNghiep"].ToString();
                list.Add(dto);
            }
            if (list.Count == 0)
                return true;
            else
                return false;
        }
    }
}
